package com.hao.tool.extra.cloud.module.youtu.fac;


import com.hao.tool.common.util.HttpClientUtil;
import com.hao.tool.common.util.JsonKitUtil;
import com.hao.tool.common.util.NetworkUtil;
import com.hao.tool.extra.cloud.module.youtu.entity.YtAccessToken;
import com.youtu.sign.YoutuSign;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * description
 *
 * @author RuiHaoZ on CMHK.jx
 * @date 2020-07-30 13:33
 */
public class YouTuFactory {

    private YtAccessToken txAccess;

    public YouTuFactory(YtAccessToken txAccess) {
        this.txAccess = txAccess;
    }


    public String ocrBankCard(String image, String imageUrl) throws IOException {
        Map<String, String> params = new HashMap<>();
        params.put("app_id", txAccess.getAppId());
        params.put("image", image);
        params.put("url", imageUrl);
        params.put("card_type", "2");
        params.put("session_id", NetworkUtil.getLocalMac());
        return build().sendPostFromRow("https://api.youtu.qq.com/youtu/ocrapi/creditcardocr",
                JsonKitUtil.getString(params));
    }

    public String ocrIdCard(String image, String imageUrl) throws IOException {
        return ocr(image, imageUrl, "https://api.youtu.qq.com/youtu/ocrapi/idcardocr");
    }

    public String ocrPlate(String image, String imageUrl) throws IOException {
        return ocr(image, imageUrl, "https://api.youtu.qq.com/youtu/ocrapi/plateocr");
    }


    /**
     * @param image
     * @param imageUrl
     * @param type     识别类型，0表示行驶证识别，1表示驾驶证识别，2表示行驶证副页识别，3表示驾驶证副页
     * @return
     * @throws IOException
     */
    public String ocrDriver(String image, String imageUrl, Integer type) throws IOException {

        Map<String, Object> params = new HashMap<>();
        params.put("app_id", txAccess.getAppId());
        params.put("image", image);
        params.put("url", imageUrl);
        params.put("type", type == null ? 0 : type);
        params.put("session_id", NetworkUtil.getLocalMac());
        return build().sendPostFromRow("https://api.youtu.qq.com/youtu/ocrapi/driverlicenseocr",
                JsonKitUtil.getString(params));
    }


    private HttpClientUtil build() {

        StringBuffer youtuSign = new StringBuffer();
        YoutuSign.appSign(txAccess.getAppId(), txAccess.getSecretId(),
                txAccess.getSecretKey(),
                System.currentTimeMillis() / 1000 + 2592000, "916783145", youtuSign);

        Map<String, String> headMap = new HashMap<>();
        headMap.put("Host", "api.youtu.qq.com");
        headMap.put("Content-Type", "text/json");
        headMap.put("Authorization", youtuSign.toString());

        return HttpClientUtil.build().setLogStatus(false).setHeaders(headMap);

    }

    private String ocr(String image, String imageUrl, String url) throws IOException {
        Map<String, Object> params = new HashMap<>();
        params.put("app_id", txAccess.getAppId());
        params.put("image", image);
        params.put("url", imageUrl);
        params.put("session_id", NetworkUtil.getLocalMac());
        return build().sendPostFromRow(url,
                JsonKitUtil.getString(params));
    }


}

